﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Cryomancer.WPF.CustomControls
{
	[TemplatePart(Name="Part_AddButton", Type=typeof(Button))]
	public class TabControlAddable : TabControl
	{
		#region Events
		
		#region TabAdded Event Code
		public static RoutedEvent TabAddedEvent;
		public static RoutedEvent PreviewTabAddedEvent;

		public event TabAddedRoutedEventHandler TabAdded
		{
			add    { AddHandler(TabAddedEvent, value); }
			remove { RemoveHandler(TabAddedEvent, value); }
		}

		public event TabAddedRoutedEventHandler PreviewTabAdded
		{
			add    { AddHandler(PreviewTabAddedEvent, value); }
			remove { RemoveHandler(PreviewTabAddedEvent, value); }
		}

		protected virtual void OnTabAdded(TabItemClosable tabItem)
		{
			TabAddedRoutedEventArgs args = new TabAddedRoutedEventArgs(PreviewTabAddedEvent, tabItem);

			RaiseEvent(args);
			if (!args.Handled)
			{
				args = new TabAddedRoutedEventArgs(TabAddedEvent, tabItem);
				RaiseEvent(args);
			}
		}
		#endregion	

		#endregion

		#region Members
		#endregion

		#region Properties
		#endregion

		#region C'Tors and other object lifetime methods
		static TabControlAddable()
		{
			TabAddedEvent = EventManager.RegisterRoutedEvent(
				"TabAdded",
				RoutingStrategy.Bubble,
				typeof(TabAddedRoutedEventHandler),
				typeof(TabControlAddable));

			PreviewTabAddedEvent = EventManager.RegisterRoutedEvent(
				"PreviewTabAdded",
				RoutingStrategy.Tunnel,
				typeof(TabAddedRoutedEventHandler),
				typeof(TabControlAddable));

			DefaultStyleKeyProperty.OverrideMetadata(typeof(TabControlAddable), new FrameworkPropertyMetadata(typeof(TabControlAddable)));
		}
		#endregion

		#region Methods

		#region Public Methods
		public TabItemClosable AddTab(string header)
		{
			TabItemClosable newTab = new TabItemClosable();
			newTab.Header = header;

			OnTabAdded(newTab);
			
			this.Items.Add(newTab);

			return newTab;
		}
		#endregion

		#region Control Overrides

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();

			//Tie the Add Button's click event to the handler
			Button addButton = GetTemplateChild("Part_AddButton") as Button;
			addButton.Click += new RoutedEventHandler(AddButton_Click);
		}

		#endregion

		#region Private Methods 
		
		#endregion

		#endregion

		#region Event Handlers
		private void AddButton_Click(object sender, RoutedEventArgs e)
		{
			AddTab("New Tab");
		}
		#endregion
	}

	public delegate void TabAddedRoutedEventHandler(object sender, TabAddedRoutedEventArgs args);
	public class TabAddedRoutedEventArgs : RoutedEventArgs
	{
		#region Members
		private TabItemClosable tabItem;
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the Tab Item
		/// </summary>
		public TabItemClosable TabItem
		{
			get
			{
				return this.tabItem;
			}
			set
			{
				this.tabItem = value;
			}
		}

		#endregion

		#region C'Tors and other object lifetime methods

		public TabAddedRoutedEventArgs(RoutedEvent routedEvent, TabItemClosable tabItem) :
			base(routedEvent)
		{
			this.tabItem = tabItem;
		}

		#endregion
	}
}
